﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace JCFrameWork.Web.Cacheing
{
    public static class CacheHelper
    {
        public delegate bool CacheGetter<TData>(out TData data);

        public static TData Get<TData>(
            CacheGetter<TData> cacheGetter,
            Func<TData> sourceGetter,
            Action<TData> cacheSetter)
        {
            TData data;
            if (cacheGetter(out data))
            {
                return data;
            }

            data = sourceGetter();
            cacheSetter(data);

            return data;
        }

        public static string GenericCacheKey(params object[] args)
        {
            return String.Join("_", args.Select(a => a == null ? "null" : a.ToString()).ToArray());
        }
    }
}
